//
//  FeedbackVc.m
//  WY_Milk
//
//  Created by wangyang on 2020/2/3.
//  Copyright © 2020 wangyang. All rights reserved.
//

#import "FeedbackVc.h"

@interface FeedbackVc ()<UITextViewDelegate,UIActionSheetDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate>

@end

@implementation FeedbackVc

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title = @"意见反馈";
     self.navigationController.interactivePopGestureRecognizer.delegate = self;
    self.view.backgroundColor =   UIColorFromRGB(0xF5F7F9);
    
    [self initSetUI];
}
-(void)initSetUI
{
    
    _OneView = [[UIView alloc] init];
    [self.view addSubview:_OneView];
    _OneView.layer.backgroundColor = [UIColor colorWithRed:255/255.0 green:255/255.0 blue:255/255.0 alpha:1.0].CGColor;
    _OneView.layer.cornerRadius = 14;
    _OneView.layer.shadowColor = [UIColor colorWithRed:0/255.0 green:0/255.0 blue:0/255.0 alpha:0.06].CGColor;
    _OneView.layer.shadowOffset = CGSizeMake(0,1);
    _OneView.layer.shadowOpacity = 1;
    _OneView.layer.shadowRadius = 3;
    [_OneView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.equalTo(CGSizeMake(WySCREENWIDTH-34, 175));
        make.top.mas_offset(10);
        make.centerX.mas_offset(0);
    }];
    
    _TwoView = [[UIView alloc] init];
    [self.view addSubview:_TwoView];
    _TwoView.layer.backgroundColor = [UIColor colorWithRed:255/255.0 green:255/255.0 blue:255/255.0 alpha:1.0].CGColor;
    _TwoView.layer.cornerRadius = 14;
    _TwoView.layer.shadowColor = [UIColor colorWithRed:0/255.0 green:0/255.0 blue:0/255.0 alpha:0.06].CGColor;
    _TwoView.layer.shadowOffset = CGSizeMake(0,1);
    _TwoView.layer.shadowOpacity = 1;
    _TwoView.layer.shadowRadius = 3;
    [_TwoView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.equalTo(CGSizeMake(WySCREENWIDTH-34, 109));
        make.top.equalTo(self.OneView.bottom).offset(10);
        make.centerX.mas_offset(0);
    }];
    
    
    
    _WyTextView = [[UITextView alloc]init];
    [self.OneView addSubview:_WyTextView];
    _WyTextView.delegate = self;
    _WyTextView.text = @"通过这次体验说说你的感受和建议吧～";
    _WyTextView.textColor  = [UIColor grayColor];
    _WyTextView.editable = YES;
    _WyTextView.userInteractionEnabled = YES;
    _WyTextView.backgroundColor = [UIColor whiteColor];
    _WyTextView.font = [UIFont systemFontOfSize:16];
    [_WyTextView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.equalTo(CGSizeMake(WySCREENWIDTH-54, 155));
        make.top.mas_offset(10);
        make.centerX.mas_offset(0);
    }];
    
    _WycameImg = [UIImageView imageWithImgName:@"上传照片" superView:_TwoView];
    [_WycameImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.equalTo(CGSizeMake(BiLiWidth(80), BiLiWidth(80)));
        make.top.mas_offset(BiLiWidth(15));
        make.right.equalTo(BiLiWidth(-11));
    }];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapselector)];
    _WycameImg.userInteractionEnabled = YES;
    [self.WycameImg addGestureRecognizer:tap];
    
    
    _WyBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.view addSubview:_WyBtn];
    [_WyBtn setBackgroundColor: UIColorFromRGB(0xFADB14)];
    [_WyBtn setTitle:@"提交" forState:0];
    [_WyBtn setTitleColor:[UIColor whiteColor] forState:0];
    [_WyBtn addTarget:self action:@selector(Submitselector) forControlEvents:UIControlEventTouchUpInside];
    _WyBtn.layer.cornerRadius = 8;
    [_WyBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.equalTo(CGSizeMake(WySCREENWIDTH-30, 45));
        make.top.equalTo(self.TwoView.bottom).offset(40);
        make.centerX.mas_offset(0);
    }];
    
    
}
-(void)textViewDidBeginEditing:(UITextView *)textView
{
    if ([_WyTextView.text isEqualToString:@"通过这次体验说说你的感受和建议吧～"]) {
        _WyTextView.text = @"";
    }
    
}
-(void)tapselector
{
    UIActionSheet* actionSheet = [[UIActionSheet alloc]initWithTitle:@"选择图片" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"相机",@"相册", nil];
    [actionSheet showInView:self.view];
}
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    switch (buttonIndex) {
        case 0:
        {
            if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
            {
                UIImagePickerController* imgPicker = [[UIImagePickerController alloc]init];
                imgPicker.delegate = self;
                imgPicker.allowsEditing = YES;
                imgPicker.sourceType = UIImagePickerControllerSourceTypeCamera;
                [self presentViewController:imgPicker animated:YES completion:nil];
            }
            else
            {
                UIAlertView* alert = [[UIAlertView alloc]initWithTitle:@"信息提示" message:@"相机不能工作" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
                [alert show];
            }
        }
            break;
        case 1:
        {
            if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary])
            {
                UIImagePickerController* imgPicker2 = [[UIImagePickerController alloc]init];
                imgPicker2.delegate = self;
                imgPicker2.allowsEditing = YES;
                //imgPicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;//默认值相册
                [self presentViewController:imgPicker2 animated:YES completion:nil];
            }
            else
            {
                UIAlertView* alert = [[UIAlertView alloc]initWithTitle:@"信息提示" message:@"相册不能使用" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles: nil];
                [alert show];
            }
        }
            break;
        default:
            break;
    }
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
{
    
    _WycameImg.image = [info objectForKey:UIImagePickerControllerOriginalImage];
    
    UIImageWriteToSavedPhotosAlbum(_WycameImg.image, nil, nil, nil);
    [self dismissViewControllerAnimated:YES completion:nil];
    
}

-(void)Submitselector

{
    if (_WyTextView.text.length == 0) {
        
        UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"信息提示"
                                                                       message:@"请输入内容"
                                                                preferredStyle:UIAlertControllerStyleAlert];
        
        UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive
                                                              handler:^(UIAlertAction * action) {
                                                                  //响应事件
                                                                  NSLog(@"action = %@", action);
                                                              }];
        UIAlertAction* cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault
                                                             handler:^(UIAlertAction * action) {
                                                                 //响应事件
                                                                 NSLog(@"action = %@", action);
                                                             }];
        
        [alert addAction:defaultAction];
        [alert addAction:cancelAction];
        [self presentViewController:alert animated:YES completion:nil];
        
    }
    
    if (_WycameImg.image == nil) {
        UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"信息提示"
                                                                       message:@"请输入内容"
                                                                preferredStyle:UIAlertControllerStyleAlert];
        
        UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive
                                                              handler:^(UIAlertAction * action) {
                                                                  //响应事件
                                                                  NSLog(@"action = %@", action);
                                                              }];
        UIAlertAction* cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault
                                                             handler:^(UIAlertAction * action) {
                                                                 //响应事件
                                                                 NSLog(@"action = %@", action);
                                                             }];
        
        [alert addAction:defaultAction];
        [alert addAction:cancelAction];
        [self presentViewController:alert animated:YES completion:nil];
    }
    
    if (_WyTextView.text.length >0 && _WycameImg.image >nil) {
        UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"信息提示"
                                                                       message:@"提交成功"
                                                                preferredStyle:UIAlertControllerStyleAlert];
        
        UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive
                                                              handler:^(UIAlertAction * action) {
                                                                  //响应事件
                                                                  [self.navigationController popViewControllerAnimated:YES];
                                                                  
                                                                  NSLog(@"action = %@", action);
                                                              }];
        UIAlertAction* cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault
                                                             handler:^(UIAlertAction * action) {
                                                                 //响应事件
                                                                 NSLog(@"action = %@", action);
                                                             }];
        
        [alert addAction:defaultAction];
        [alert addAction:cancelAction];
        [self presentViewController:alert animated:YES completion:nil];
    }
    else
    {
        UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"信息提示"
                                                                       message:@"提交失败"
                                                                preferredStyle:UIAlertControllerStyleAlert];
        
        UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive
                                                              handler:^(UIAlertAction * action) {
                                                                  //响应事件
                                                                  NSLog(@"action = %@", action);
                                                              }];
        UIAlertAction* cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault
                                                             handler:^(UIAlertAction * action) {
                                                                 //响应事件
                                                                 NSLog(@"action = %@", action);
                                                             }];
        
        [alert addAction:defaultAction];
        [alert addAction:cancelAction];
        [self presentViewController:alert animated:YES completion:nil];
    }
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
